﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Mapenect.Data
{
    public class FixedQueueList<T> : List<T>, IList<T>
    {
        protected int maxCount;

        public FixedQueueList(int max)
        {
            this.maxCount = max;
        }

        new public int IndexOf(T item)
        {
            return base.IndexOf(item);
        }

        new public void Insert(int index, T item)
        {
            base.Insert(index, item);
        }

        public new T this[int index]
        {
            get
            {
                try
                {
                    return (T)this[index];
                }
                catch (StackOverflowException ex)
                {
                    throw new StackOverflowException("Wert konnte nicht gelesen werden.", ex);
                }
                catch
                {
                    return default(T);
                }
            }
            set
            {
                try
                {
                    this[index] = value;
                }
                catch (StackOverflowException ex)
                {
                    throw new StackOverflowException("Wert konnte nicht geschrieben werden.", ex);
                }
            }
        }

        new public void Add(T item)
        {
            if (this.maxCount == this.Count)
                this.RemoveAt(0);
            
            base.Add(item);
        }

        new public bool Contains(T item)
        {
            return base.Contains(item);
        }

        new public void CopyTo(T[] array, int arrayIndex)
        {
            base.CopyTo(array, arrayIndex);
        }

        new public bool Remove(T item)
        {
            if (this.Contains(item))
            {
                base.Remove(item);
                return true;
            }
            else
            {
                return false;
            }
        }

        public new IEnumerator<T> GetEnumerator()
        {
            return (IEnumerator<T>)base.GetEnumerator();
        }

        public T GetLastElement()
        {
            if (base.Count > 0)
            {
                return base[base.Count - 1];
            }
            else
            {
                return default(T);
            }
        }
    }
}
